package com.syh.jn.scm.domain.upstream.entity.finance;

import cn.redscarf.commons.spring.SpringContextUtils;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.domain.upstream.repository.finance.UpStatementAccountBillReadRepository;
import com.syh.jn.scm.exception.BusinessException;
import com.syh.jn.scm.service.api.upstream.finance.enums.UpReceiptBillEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;

/**
 * 上游收款单
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class UpReceiptBill extends AbstractSimpleAggregate<Long> {
  /**
   * 付款单编号
   */
  private String code;
  /**
   * 对账单id
   */
  private Long statementOfAccountId;
  /**
   * 客户类型: 0上游, 1下游, 2物流
   */
  private Short customerType;
  /**
   * 锁定状态(0 未锁定 1 已锁定 )双方确认后自动锁定，锁定后为最终状态，将不可再修改
   */
  private Short lockStatus;
  /**
   * 付款单状态:0未到账, 1已到账
   */
  private Short receiptState;
  /**
   * 录单人id
   */
  private Long brokerageId;
  /**
   * 客户id
   */
  private Long customerId;
  /**
   * 我方开户行
   */
  private String ourAccountBank;
  /**
   * 对方开户行
   */
  private String otherPartyAccountBank;
  /**
   * 我方开户支行
   */
  private String ourAccountBranchBank;
  /**
   * 对方开户支行
   */
  private String otherPartyAccountBranchBank;
  /**
   * 我方开户账号
   */
  private String ourAccountBankCode;
  /**
   * 对方开户账号
   */
  private String otherPartyAccountBankCode;
  /**
   * 我方地址省 市 区
   */
  private String ourArea;
  /**
   * 对方地址省 市 区
   */
  private String otherPartyArea;
  /**
   * 我方支行地址
   */
  private String ourBranchBankAddress;
  /**
   * 对方支行地址
   */
  private String otherPartyBranchBankAddress;
  /**
   * 付款金额
   */
  private BigDecimal price;
  /**
   * 付款日期
   */
  private Long inputTimestamp;
  /**
   * 银行流水号
   */
  private String bankStatement;
  /**
   * 备注
   */
  private String remark;
  /**
   * 对方确认是否到账：0未到账, 1已到账
   */
  private Short confirmState;
  /**
   * 归属端id
   */
  private Long businessEntityId;

  /**
   * 确认到账
   */
  public void confirmState() {
    this.checkMoney();
    this.confirmState = UpReceiptBillEnum.OTHERPARTY_CONFIRM.getStatus();
    if (this.receiptState.equals(UpReceiptBillEnum.OUR_CONFIRM.getStatus()))
      this.lockStatus = UpReceiptBillEnum.LOCK.getStatus();
  }

  /**
   * 取消到账
   */
  public void cancel() {
    if (this.lockStatus.equals(UpReceiptBillEnum.LOCK.getStatus())) {
      throw new BusinessException("收款单为锁定状态,不可以取消");
    }
    this.confirmState = UpReceiptBillEnum.OTHERPARTY_STAYCONFIRM.getStatus();
  }

  /**
   * 检查对账单金额
   */
  public void checkMoney() {
    if (this.price.compareTo(BigDecimal.ZERO) <= 0) {
      throw new BusinessException("开票金额不正确，请检查！");
    }
    UpStatementAccountBillReadRepository statementBillRead = (UpStatementAccountBillReadRepository) SpringContextUtils.getBean(UpStatementAccountBillReadRepository.class);
    UpStatementAccountBill statement = statementBillRead.getById(this.statementOfAccountId);

    BigDecimal notPaymentAmt = statement.getPrice().subtract(statement.getMakeCollectionsPrice());
    if (this.price.compareTo(notPaymentAmt) > 0) {
      throw new BusinessException("当前对账单未支付金额为：" + notPaymentAmt + "元。付款金额不能大于未付金额！");
    }
  }
}
